package com.lanou3g.mynewstest.module.news.headline;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.beans.NewsDetailBeans;
import com.lanou3g.mynewstest.beans.NewsHeadlineBeans;
import com.lanou3g.mynewstest.beans.NewsHeadlineSpecialBeans;
import com.lanou3g.mynewstest.beans.NewsPhotoSetBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;
import com.lanou3g.mynewstest.tools.StringRequestUtf;


/**
 * Created by dllo on 16/1/8.
 */
public class HeadlineAdapter extends RecyclerView.Adapter {
    private Context context;
    private HeadlineADSAdapter adapter;
    private NewsHeadlineBeans data;
    private int imageViews[] = {R.id.ads_iv1, R.id.ads_iv2, R.id.ads_iv3, R.id.ads_iv4};
    private NewsPhotoSetBeans beans;
    private NewsDetailBeans detailBeans;
    private static final String URL_HEAD = "http://c.m.163.com/nc/article/";
    private static final String URL_FOOT = "/full.html";

    public HeadlineAdapter(Context context, NewsHeadlineBeans beans) {
        this.context = context;
        this.data = beans;
        adapter = new HeadlineADSAdapter(context, data);
    }


    @Override
    public int getItemViewType(int position) {
        if (data.getT1348647909107().get(position).getAds() != null) {
            return 1;
        } else if (data.getT1348647909107().get(position).getImgextra() != null) {
            return 2;
        } else if (data.getT1348647909107().get(position).getImgType() == 1) {
            return 3;
        } else {
            return 4;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 1:
                View view1 = LayoutInflater.from(context).inflate(R.layout.item_headline_ads_vp, parent, false);
                MyAdsViewHolder holder1 = new MyAdsViewHolder(view1);
                return holder1;
            case 2:
                View view2 = LayoutInflater.from(context).inflate(R.layout.item_headline_threepic, parent, false);
                MyThreePicViewHolder holder2 = new MyThreePicViewHolder(view2);
                return holder2;
            case 3:
                View view3 = LayoutInflater.from(context).inflate(R.layout.item_headline_onebigpic, parent, false);
                MyBigPicViewHolder holder3 = new MyBigPicViewHolder(view3);
                return holder3;
            case 4:
                View view4 = LayoutInflater.from(context).inflate(R.layout.item_headline_normal, parent, false);
                MyNormalViewHolder holder4 = new MyNormalViewHolder(view4);
                return holder4;
            default:
                return null;
        }

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final int pos[] = {holder.getLayoutPosition()};
        switch (holder.getItemViewType()) {

            case 1:

                break;
            case 2:
                ((MyThreePicViewHolder) holder).title.setText(data.getT1348647909107().get(position).getTitle());
                ((MyThreePicViewHolder) holder).replyCount.setText("跟帖" + data.getT1348647909107().get(position).getReplyCount());
                MyRequestQueue.setNetImage(((MyThreePicViewHolder) holder).imageView1, data.getT1348647909107().get(position).getImgsrc());
                MyRequestQueue.setNetImage(((MyThreePicViewHolder) holder).imageView2, data.getT1348647909107().get(position).getImgextra().get(0).getImgsrc());
                MyRequestQueue.setNetImage(((MyThreePicViewHolder) holder).imageView3, data.getT1348647909107().get(position).getImgextra().get(1).getImgsrc());
                ((MyThreePicViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        goToPhotoSet(pos[0]);
                    }
                });
                break;
            case 3:
                ((MyBigPicViewHolder) holder).title.setText(data.getT1348647909107().get(position).getTitle());
                ((MyBigPicViewHolder) holder).digest.setText(data.getT1348647909107().get(position).getDigest());
                ((MyBigPicViewHolder) holder).replyCount.setText("跟帖" + data.getT1348647909107().get(position).getReplyCount());
                MyRequestQueue.setNetImage(((MyBigPicViewHolder) holder).imageView, data.getT1348647909107().get(position).getImgsrc());

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        goToDetail(pos[0]);
                    }
                });
                break;
            case 4:
                ((MyNormalViewHolder) holder).title.setText(data.getT1348647909107().get(position).getTitle());
                ((MyNormalViewHolder) holder).digest.setText(data.getT1348647909107().get(position).getDigest());
                if (data.getT1348647909107().get(position).getSkipType() == null) {
                    ((MyNormalViewHolder) holder).replyCount.setText("跟帖" + data.getT1348647909107().get(position).getReplyCount());
                    ((MyNormalViewHolder) holder).replyCount.setVisibility(View.VISIBLE);
                    ((MyNormalViewHolder) holder).special.setVisibility(View.INVISIBLE);

                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            goToDetail(pos[0]);
                        }
                    });
                } else if (data.getT1348647909107().get(position).getSkipType().equals("special")) {
                    ((MyNormalViewHolder) holder).replyCount.setVisibility(View.INVISIBLE);
                    ((MyNormalViewHolder) holder).special.setVisibility(View.VISIBLE);
                    ((MyNormalViewHolder) holder).relativeLayout.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                            StringRequest specialRequest = new StringRequest("http://c.3g.163.com/nc/special/" + data.getT1348647909107().get(pos[0]).getSkipID() + ".html",
                                    new Response.Listener<String>() {
                                        @Override
                                        public void onResponse(String response) {
                                            response = response.replaceFirst(data.getT1348647909107().get(pos[0]).getSkipID(), "special");
                                            Gson gson = new Gson();
                                            NewsHeadlineSpecialBeans beans = gson.fromJson(response, NewsHeadlineSpecialBeans.class);
                                            SpecialActivity.goToSpecialActivity(context, beans);
                                        }
                                    },
                                    new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {

                                        }
                                    });
                            MyRequestQueue.addRequest(specialRequest);

                        }
                    });
                } else if (!data.getT1348647909107().get(position).getSkipType().equals("special")) {
                    ((MyNormalViewHolder) holder).replyCount.setText("跟帖" + data.getT1348647909107().get(position).getReplyCount());
                    ((MyNormalViewHolder) holder).replyCount.setVisibility(View.VISIBLE);
                    ((MyNormalViewHolder) holder).special.setVisibility(View.INVISIBLE);
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(context, "不是专题", Toast.LENGTH_SHORT).show();
                        }
                    });
                }


                MyRequestQueue.setNetImage(((MyNormalViewHolder) holder).imageView, data.getT1348647909107().get(position).getImgsrc());
                break;
        }
    }

    @Override
    public int getItemCount() {
        return data.getT1348647909107().size();
    }

    public void addData(NewsHeadlineBeans beans) {
        this.data=beans;
        notifyDataSetChanged();
    }


    class MyAdsViewHolder extends RecyclerView.ViewHolder {
        private ViewPager viewPager;
        View view;

        public MyAdsViewHolder(View itemView) {
            super(itemView);
            view = itemView;

            viewPager = (ViewPager) itemView.findViewById(R.id.item_headline_ads_viewpager);

            viewPager.setAdapter(adapter);
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    int index = position % 4;
                    for (int i = 0; i < 4; i++) {
                        if (i == index) {
                            ImageView selectedImage = (ImageView) view.findViewById(imageViews[index]);

                            selectedImage.setImageResource(R.mipmap.ads_select);
                        } else {
                            ImageView selectedImage = (ImageView) view.findViewById(imageViews[i]);
                            selectedImage.setImageResource(R.mipmap.ads);
                        }
                    }

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

        }
    }

    class MyNormalViewHolder extends RecyclerView.ViewHolder {
        TextView title, digest, replyCount;
        NetworkImageView imageView;
        ImageView special;
        RelativeLayout relativeLayout;

        public MyNormalViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.item_headline_normal_title);
            digest = (TextView) itemView.findViewById(R.id.item_headline_normal_digest);
            replyCount = (TextView) itemView.findViewById(R.id.item_headline_normal_replyCount);
            imageView = (NetworkImageView) itemView.findViewById(R.id.item_headline_normal_iv);
            special = (ImageView) itemView.findViewById(R.id.item_headline_normal_type_iv);
            relativeLayout = (RelativeLayout) itemView.findViewById(R.id.item_headline_normal_relativelayout);
        }
    }

    class MyBigPicViewHolder extends RecyclerView.ViewHolder {
        TextView title, digest, replyCount;
        NetworkImageView imageView;

        public MyBigPicViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.item_headline_bigpic_title);
            digest = (TextView) itemView.findViewById(R.id.item_headline_bigpic_digest);
            replyCount = (TextView) itemView.findViewById(R.id.item_headline_bigpic_replyCount);
            imageView = (NetworkImageView) itemView.findViewById(R.id.item_headline_bigpic_iv);
        }
    }

    class MyThreePicViewHolder extends RecyclerView.ViewHolder {
        TextView title, replyCount;
        NetworkImageView imageView1, imageView2, imageView3;

        public MyThreePicViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.item_headline_threepic_title);
            replyCount = (TextView) itemView.findViewById(R.id.item_headline_threepic_replyCount);
            imageView1 = (NetworkImageView) itemView.findViewById(R.id.item_headline_threepic_iv1);
            imageView2 = (NetworkImageView) itemView.findViewById(R.id.item_headline_threepic_iv2);
            imageView3 = (NetworkImageView) itemView.findViewById(R.id.item_headline_threepic_iv3);
        }
    }

    private void goToPhotoSet(int pos) {
        String url = data.getT1348647909107().get(pos).getPhotosetID();
        url = url.replace("|", "/");
        url = url.replace(url.substring(0, 4), "");

        StringRequestUtf request = new StringRequestUtf("http://c.m.163.com/photo/api/set/" + url + ".json",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        beans = gson.fromJson(response, NewsPhotoSetBeans.class);
                        PhotoSetActivity.goToPhotoSetActivity(context, beans);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        MyRequestQueue.addRequest(request);
    }

    private void goToDetail(final int pos) {
        String url = data.getT1348647909107().get(pos).getDocid();
        url = URL_HEAD + url + URL_FOOT;

        StringRequestUtf request = new StringRequestUtf(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Gson gson = new Gson();
                        response = response.replace(data.getT1348647909107().get(pos).getDocid(), "postID");
                        detailBeans = gson.fromJson(response, NewsDetailBeans.class);

                        DetailActivity.goToDetailActivity(context, detailBeans);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        MyRequestQueue.addRequest(request);
    }
}
